package str;

import org.junit.Test;

/**
 * Created mangcao with 2019/2/23 17:25
 * Description:
 */
@SuppressWarnings("ALL")
public class StrTest {


    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("ss");

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("ss");

        String string = new String("ss");
        string.hashCode();
        string.equals("x");

    }


    @Test
    public void Test(){
        System.out.println("ClassLodarDemo's ClassLoader is " + StrTest.class.getClassLoader());
        System.out.println("The Parent of ClassLodarDemo's ClassLoader is " + StrTest.class.getClassLoader().getParent());
        System.out.println("The GrandParent of ClassLodarDemo's ClassLoader is " + StrTest.class.getClassLoader().getParent().getParent());

    }

    @Test
    public void _equalTest2(){
        String str1 = "str";
        String str2 = "ing";

        String str3 = "str" + "ing";//常量池中的对象

        // 如果是有引用对象在做字符串拼接，都在在堆上创建
        String str4 = str1 + str2; //在堆上创建的新的对象
        String str5 = "string";//常量池中的对象
        System.out.println(str3 == str4);//false
        System.out.println(str3 == str5);//true
        System.out.println(str4 == str5);//false

    }

    @Test
    public void _equalTest(){
        //先检查字符串常量池中有没有"abcd"，如果字符串常量池中没有，则创建一个，然后 str1 指向字符串常量池中的对象，如果有，则直接将 str1 指向"abcd""；
        String str1 = "abcd";

        //堆中创建一个新的对象
        String str2 = new String("abcd");

        //堆中创建一个新的对象
        String str3 = new String("abcd");

        System.out.println(str1==str2);//false
        System.out.println(str2==str3);//false
        System.out.println(str1.equals(str2));//true

    }

    @Test
    public void StTs(){

        String s = new String("1");
        s.intern();
        String s2 = "1";
        System.out.println(s == s2);

        String s3 = new String("1") + new String("1");
        s3.intern();
        String s4 = "11";
        System.out.println(s3 == s4);

        s.intern();

    }



}
